package org.fu.crowd.controller;

import org.fu.crowd.api.MySQLRemoteService;
import org.fu.crowd.constant.CrowdConstant;
import org.fu.crowd.utils.Common;
import org.fu.crowd.vo.AddressVO;
import org.fu.crowd.vo.MemberLoginV0;
import org.fu.crowd.vo.OrderProjectVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OrderController {


    @Resource
    private MySQLRemoteService mySQLRemoteService;



    @RequestMapping("/order/confirmReturnInfo")
    public String orderConfirmReturnInfo(@RequestParam("projectId")Integer projectId,
                                         @RequestParam("returnId")Integer returnId, HttpSession session){
        Common<OrderProjectVO> common = mySQLRemoteService.orderConfirmReturnInfo(projectId,returnId);
        System.out.println(common.getData());
        if (Common.SUCCESS.equals(common.getResult())){
            session.setAttribute("orderProjectVO",common.getData());
        }
        return "orderPay-1";
    }




    @RequestMapping("/order/ConfirmOrder/{returnCount}")
    public String orderConfirmOrder(@PathVariable("returnCount")Integer returnCount,HttpSession session){
        //获取上个页面存入session域的对象，更新回报数量
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute("orderProjectVO");
        orderProjectVO.setReturnCount(returnCount);
        session.setAttribute("orderProjectVO",orderProjectVO);
        //获取当前用户id
        MemberLoginV0 memberLoginV0 = (MemberLoginV0) session.getAttribute(CrowdConstant.LOGIN_MEMBER);
        Integer memberId = memberLoginV0.getId();
        //查询目前收获地址数据
        Common<List<AddressVO>> commonAddressVO = mySQLRemoteService.getAddressVORemote( memberId);
        if (Common.SUCCESS.equals(commonAddressVO.getResult())){
            List<AddressVO> addressVOList = commonAddressVO.getData();
            session.setAttribute("addressVOList",addressVOList);
        }
        return "orderPay-2";
    }





    @RequestMapping("/order/saveAddress")
    public String orderSaveAddress(AddressVO addressVO , HttpSession session){
        //执行保存地址信息
        Common<String> common = mySQLRemoteService.saveAddressVORemote(addressVO);
        //重定向到orderPay-2
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute("orderProjectVO");
        Integer returnCount = orderProjectVO.getReturnCount();
        return "redirect:/order/ConfirmOrder/"+returnCount;
    }


}
